/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "prs_FrmFrameStyle.h"
#include "prs_FrmScrollBarStyle.h"
#include "prs_FrmNodes.h"
#include "prs_Unit.h"

using namespace prs;

// TRuleFrmFrameStyle /////////////////////////////////////////////////////

//
TRuleFrmFrameStyle::TRuleFrmFrameStyle(
  snt::text::TContext &Context,
  svl::TVariant &VParentNode,
  TRuleFrmStyleState &RStyleState
) 
:vParentNode(VParentNode)
,rStyleState(RStyleState)
{
  snt::TReturn Return;
  snt::TFVoid F;
  snt::text::TSequence T(Context);
  snt::text::TIdent    I(Context);
  snt::text::TNumber   N(Context);
  snt::text::TKeyword  K(Context);
  prs::TFuncNewNodeIntoAttrOldChildren FNewNode;
	
  this->Name(GetClassName()).Local(vParentNode)
  | K.New("frame") & FNewNode.New(VParentNode,TNodeFrmFrameStyle()) & T.New("(") & A.New()
  ;
  
  A.Name(GetClassName() + "[A]")
  | T.New("frame")		 & F.New(this,&TRuleFrmFrameStyle::FFrame)		 & rStyleState.New() & A.New()
  | T.New("panelScroll") & F.New(this,&TRuleFrmFrameStyle::FPanelScroll) & rStyleState.New() & A.New()
  | T.New("client")		 & F.New(this,&TRuleFrmFrameStyle::FClient)		 & rStyleState.New() & A.New()
  | T.New("vScrollBar") & T.New("=") & I.NewClosure(this,&TRuleFrmFrameStyle::FVScrollBar) & A.New()
  | T.New("hScrollBar") & T.New("=") & I.NewClosure(this,&TRuleFrmFrameStyle::FHScrollBar) & A.New()
  | T.New(")") & Return.New()
  ;
	
}

bool TRuleFrmFrameStyle::FHScrollBar(snt::text::TIdent::TFuncClosureDoEntryParams &Params){
	TNodeFrmFrameStyle* FStyle = dynamic_cast<TNodeFrmFrameStyle*>(vParentNode.Pointer());
	
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(
				TNodeSearch(FStyle)
				.SearchNearParentByType(TNodeFrm::nodeType_Unit)
				.GetFirst() );
	
	TNode* style = Unit->SearchStyleById( Params.Ident.GetValue().String() );
	
	if( !style ){
		Params.OutError << "Style " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}

	TNodeFrmHScrollBarStyle *hScrollBar = dynamic_cast<TNodeFrmHScrollBarStyle*>(
				TNodeSearch(style)
				.SearchChildren()
				.SearchByType(TNodeFrm::nodeType_HScrollBar)
				.GetFirst() );
	
	if( !hScrollBar ){
		Params.OutError << "hScrollBar style in " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}	
	
	FStyle->Style.Value.HScroll = hScrollBar->Style.Value;
	
	return true;
}

bool TRuleFrmFrameStyle::FVScrollBar(snt::text::TIdent::TFuncClosureDoEntryParams &Params){
	TNodeFrmFrameStyle* FStyle = dynamic_cast<TNodeFrmFrameStyle*>(vParentNode.Pointer());
	
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(
				TNodeSearch(FStyle)
				.SearchNearParentByType(TNodeFrm::nodeType_Unit)
				.GetFirst() );
	
	TNode* style = Unit->SearchStyleById( Params.Ident.GetValue().String() );
	
	if( !style ){
		Params.OutError << "Style " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}

	TNodeFrmVScrollBarStyle *vScrollBar = dynamic_cast<TNodeFrmVScrollBarStyle*>(
				TNodeSearch(style)
				.SearchChildren()
				.SearchByType(TNodeFrm::nodeType_VScrollBar)
				.GetFirst() );
	
	if( !vScrollBar ){
		Params.OutError << "vScrollBar style in " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}	
	
	FStyle->Style.Value.VScroll = vScrollBar->Style.Value;
	
	return true;
}
